package com.example.qasys.service;

import okhttp3.*;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.rmi.server.RemoteRef;
import java.util.concurrent.TimeUnit;

@Service
public class CodySentAndGet {




    public String sentMessage(String question) throws IOException {

        System.out.println(question);
//        MediaType JSON = MediaType.parse("application/json");
        MediaType mediaType = MediaType.parse("application/json");
//        String URL = "https://getcody.ai/widget-backend/95ec039a-6342-422c-b128-359f78a42d01/messages";
        question = "\"" + question + "\"";
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60,TimeUnit.SECONDS)
                .callTimeout(60,TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .build();
        String json = "{\"conversation_id\":\"rlNbWBPoayg5\",\"person_id\":\"kzPdyrxVeQro\",\"message\":"+question+"}";

        RequestBody body = RequestBody.create(mediaType, json);
//        RequestBody body = RequestBody.create(json, JSON);

        Request request = new Request.Builder()
                .url("https://getcody.ai/widget-backend/95ec039a-6342-422c-b128-359f78a42d01/messages")
                .method("POST", body)
                .addHeader("authority", "getcody.ai")
                .addHeader("accept", "application/json, text/plain, */*")
                .addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .addHeader("content-type", "application/json")
                .addHeader("origin", "https://getcody.ai")
                .addHeader("referer", "https://getcody.ai/widget/95ec039a-6342-422c-b128-359f78a42d01")
                .addHeader("sec-ch-ua", "\"Microsoft Edge\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"")
                .addHeader("sec-ch-ua-mobile", "?0")
                .addHeader("sec-ch-ua-platform", "\"Windows\"")
                .addHeader("sec-fetch-dest", "empty")
                .addHeader("sec-fetch-mode", "cors")
                .addHeader("sec-fetch-site", "same-origin")
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54")
                .build();


        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public String getAnswer() throws IOException {
        String URL = "https://getcody.ai/widget-backend/95ec039a-6342-422c-b128-359f78a42d01/messages";
        int PER_PAGE = 100;
        int PAGE = 1;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60,TimeUnit.SECONDS)
                .callTimeout(60,TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .build();
//        client.connectTimeoutMillis(60, TimeUnit.SECONDS);
        HttpUrl.Builder urlBuilder = HttpUrl.parse(URL).newBuilder();
        urlBuilder.addQueryParameter("conversation_id", "rlNbWBPoayg5");
        urlBuilder.addQueryParameter("person_id", "kzPdyrxVeQro");
        urlBuilder.addQueryParameter("per_page", String.valueOf(PER_PAGE));
        urlBuilder.addQueryParameter("page", String.valueOf(PAGE));

//        HttpUrl.Builder urlBuilder = HttpUrl.parse("https://getcody.ai/widget-backend/95ec039a-6342-422c-b128-359f78a42d01/messages").newBuilder();

        String url = urlBuilder.build().toString();
        Request request = new Request.Builder()
                .url(url)
                .addHeader("authority", "getcody.ai")
                .addHeader("accept", "application/json, text/plain, */*")
                .addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
                .addHeader("referer", "https://getcody.ai/widget/95ec039a-6342-422c-b128-359f78a42d01")
                .addHeader("sec-ch-ua", "\"Microsoft Edge\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"")
                .addHeader("sec-ch-ua-mobile", "?0")
                .addHeader("sec-ch-ua-platform", "\"Windows\"")
                .addHeader("sec-fetch-dest", "empty")
                .addHeader("sec-fetch-mode", "cors")
                .addHeader("sec-fetch-site", "same-origin")
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54")
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }
}
